package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.WxConstant;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.account.service.UserThirdIdentityService;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.notifytemp.model.dto.WxNotifyDTO;
import com.songlanyun.modules.notifytemp.service.NotifyTempService;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.rights.entity.RightsOrder;
import com.songlanyun.modules.rights.service.RightsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商家处理维权发送消息
 */
@Component
@Slf4j
public class BusinessRightsSendMessageConsumer extends BaseConsumer {

    @Autowired
    private RightsOrderService rightsOrderService;

    @Autowired
    private NotifyTempService notifyTempService;

    @Autowired
    private UserThirdIdentityService userThirdIdentityService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;


    @Override
    protected void process(Message message) {
        Long rightsOrderId = ClassConvertUtils.objToLong(new String(message.getBody()));
        RightsOrder v = rightsOrderService.getById(rightsOrderId);

        try {
            String openId = userThirdIdentityService.getOpenIdByUserId(v.getUserId(), false);
            String statusTxt = "";
            String rightsType = v.getRightsType().getMsg();
            switch (v.getRightsOperateStatus())
            {
                case AGREE:
                    statusTxt="商家已同意";
                    break;
                case REFUSE:
                    statusTxt="商家已拒绝";
                    break;
                case SHOP_RECEIPT:
                    statusTxt="商家已收货";
                    break;
                case REFUND_COMPLETE:
                    statusTxt="已退款";
                    break;
                default:
                    return;
            }

            if(StringUtils.isNotBlank(statusTxt))
            {
                //if (openId!=null){
                 //   sendMsg(v, openId, statusTxt, rightsType);
                //}

                /**
                 * 发送售后站内消息
                 */
                sendPrivateMessageMsgService.send(new MessageTO(PrivateMessageConstant.MessageType.RIGHTS_MESSAGE,
                        PrivateMessageConstant.ModelType.REFUND,v.getId()));
            }

        }
        catch (Exception ex) {
            throw  ex;
        }

    }

    private void sendMsg(RightsOrder v, String openId, String statusTxt, String rightsType) {
        /**
         * 状态
         * 订单编号
         * 售后类型
         * 订单金额
         */
        List<Object> values  = new ArrayList<>();
        values.add(statusTxt);
        values.add(v.getOrderCode());
        values.add(rightsType);
        values.add(v.getRefundMoney().toString());
        WxNotifyDTO wxNotifyDTO = new WxNotifyDTO(WxConstant.WxType.AFTER_THE_NEWS,openId,values);
        notifyTempService.sendWxNotify(wxNotifyDTO);
    }

    @RabbitListener(queues = MqConstant.BusinessRightsSendMessageConstant.BUSINESS_RIGHTS_SEND_MESSAGE_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message,channel);
    }
}
